package org.taofuxn.framework.util;

import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;

import com.taofuxn.yycreazy.R;

import org.taofuxn.framework.MainActivity;
import org.taofuxn.framework.ui.activity.SettingActivity;

/**
 * 主题控制器
 * Created by wanghuafu on 16/9/19.
 */
public class AppStyleUtil<T extends AppCompatActivity> {

    /**
     * 夜间模式的遮罩
     */
    private View mNightView;
    /**
     * 用来标识是否添加了夜间模式的
     */
    private boolean mIsAddedView;

    private WindowManager mWindowManager;

    /**
     * 主题类型枚举
     */
    public enum StyleType {
        NIGHT, LIGHT, Yellow, Palegreen, Rosybrown, Darkorchid
    }


    /**
     * 定义一个主题数组
     */
    public static StyleType themeSum[] = {
            StyleType.LIGHT,
            StyleType.NIGHT,
            StyleType.Yellow,
            StyleType.Palegreen,
            StyleType.Rosybrown,
            StyleType.Darkorchid};

    public static StyleType curStyle = StyleType.LIGHT;


    public AppStyleUtil() {

    }

    /**
     * 可以在一开始的时候加载主题
     *
     * @param baseActivity
     */
    public void aotoLoadStyleTheme(T baseActivity) {
        switch (AppStyleUtil.curStyle) {
            case LIGHT:
                baseActivity.setTheme(R.style.AppTheme_Day);
                break;
            case NIGHT:
                baseActivity.setTheme(R.style.AppTheme_Night);
                initNightView(baseActivity);
                mNightView.setBackgroundResource(R.color.night_mask);
                break;
            case Yellow:
                baseActivity.setTheme(R.style.AppTheme_Yellow);
                break;
            case Palegreen:
                baseActivity.setTheme(R.style.AppTheme_Palegreen);
                break;
            case Rosybrown:
                baseActivity.setTheme(R.style.AppTheme_Rosybrown);
                break;
            case Darkorchid:
                baseActivity.setTheme(R.style.AppTheme_Darkorchid);
                break;

        }

    }

    /**
     * 手动改变主题
     */
    public void switchThemeTo(StyleType mStyleType, T activity) {
        curStyle = mStyleType;
        if (activity instanceof MainActivity) {
            activity.recreate();
        }
        if (activity instanceof SettingActivity){
            Intent intent = new Intent(activity,MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            activity.startActivity(intent);
        }
    }

    /**
     * 初始化遮罩并且添加到窗口
     *
     * @param activity
     */
    private void initNightView(T activity) {
        if (mIsAddedView == true)
            return;
        WindowManager.LayoutParams mNightViewParam = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.TYPE_APPLICATION,
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSPARENT);
        mWindowManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
        mNightView = new View(activity);
        mWindowManager.addView(mNightView, mNightViewParam);
        mIsAddedView = true;
    }

    /**
     * 移除夜间模式的遮罩
     */
    public void onDetroy() {
        if (mIsAddedView) {
            mWindowManager.removeViewImmediate(mNightView);
            mNightView = null;
            mWindowManager = null;
        }

    }

}
